//
//  RESelectAlbumViewController.m
//  录音
//
//  Created by ywy on 2017/1/5.
//  Copyright © 2017年 Wely. All rights reserved.
//

#import "RESelectAlbumLocalViewController.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "RECheckLogin.h"
#import "REPrimaryClassification.h"// 一级分类的界面
#import "REAlbumNameCollectionViewCell.h"
#import "REAlbumNameTwoCollectionViewCell.h"// 第二个cell
#import "RESecondaryClassification.h" // 第二个model
#import "REAlbumNameThirdCollectionViewCell.h" // 第三个的cell
#import "REThirdClassification.h" // 第三个的model
#import "RETabBarViewController.h"

@interface RESelectAlbumLocalViewController ()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

// 数据源
@property(nonatomic,strong) NSMutableArray * dataArr;
//第二个数据源
@property(nonatomic,strong) NSMutableArray * dataTwoArr;
// 第三个数据源
@property(nonatomic,strong) NSMutableArray * thirdArr;
//顶部的那个deb
@property(nonatomic,strong) UICollectionView * topCollectionView;
// 中间的 那个界面的
@property(nonatomic,strong) UICollectionView * middleCollectionView;
// 中间的 那个界面的
@property(nonatomic,strong) UICollectionView * bottomCollectionView;
@property(nonatomic,assign) BOOL isSelect;

@property(nonatomic,strong) NSMutableArray * firstArr;
@property(nonatomic,strong) NSMutableArray * secondArr;
@property(nonatomic,strong) NSMutableArray * thirdDataArr;

@end

@implementation RESelectAlbumLocalViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    RETabBarViewController * tabBar = (RETabBarViewController *)self.tabBarController;
    [tabBar HideTabarView:YES];
//    self.title = @"选择专辑名称";
    UIImageView * firstImageView  =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"选择专辑名称"]];
    firstImageView.frame = CGRectMake(0, 0, 37, 17);
    self.navigationItem.titleView=firstImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.isSelect = YES;
    self.dataArr = [NSMutableArray new];
    self.dataTwoArr = [NSMutableArray new];
    self.thirdArr = [NSMutableArray new];
    [self setData];
}

-(void)setData
{
    self.firstArr  = [NSMutableArray new];
    self.secondArr = [NSMutableArray new];
    self.thirdDataArr = [NSMutableArray new];
    NSString *str = [NSString stringWithContentsOfFile:[RECheckLogin getDocumentPath:@"first"] encoding:NSUTF8StringEncoding error:nil];
    NSArray * oneArr= [str componentsSeparatedByString:@","];
    NSString *tStr = [NSString stringWithContentsOfFile:[RECheckLogin getDocumentPath:@"second"] encoding:NSUTF8StringEncoding error:nil];
    NSArray * tArr= [tStr componentsSeparatedByString:@","];
    NSString *thirdStr = [NSString stringWithContentsOfFile:[RECheckLogin getDocumentPath:@"third"] encoding:NSUTF8StringEncoding error:nil];
    NSArray * thArr= [thirdStr componentsSeparatedByString:@","];
    
    for (NSString * str in oneArr) {
        if (![self.firstArr containsObject:str]) {
            [self.firstArr addObject:str];
        }
    }
    [self.firstArr removeLastObject];
    for (NSString * str in tArr) {
        if (![self.secondArr containsObject:str]) {
            [self.secondArr addObject:str];
        }
    }
    [self.secondArr removeLastObject];
    for (NSString * str in thArr) {
        if (![self.thirdDataArr containsObject:str]) {
            [self.thirdDataArr addObject:str];
        }
    }
    [self.thirdDataArr removeLastObject];
    if (self.firstArr.count) {
           [self reqquestBaseUrl];
    }else{
        [SVProgressHUD showErrorWithStatus:@"暂时没有本地专辑！！"];
    }
}
-(void)reqquestBaseUrl
{
    __weak typeof(self) weakSelf  =  self;
    NSMutableDictionary * dict = [NSMutableDictionary new];
    dict[@"cmd"] = @"getFirstType";
    [SVProgressHUD showWithStatus:@"获取本地数据中...."];
    [RECheckLogin POST:REBaseUrl parameters:dict progress:^(CGFloat prosser) {
    } success:^(id responseObject) {
        [SVProgressHUD dismiss];
        if ([responseObject[@"result"] isEqualToString:@"0"]) {
            for (NSDictionary * dict in responseObject[@"firstTypeList"]) {
                for (NSInteger i = 0; i<weakSelf.firstArr.count; i++) {
                    if ([weakSelf.firstArr[i] isEqualToString:dict[@"firstTypeId"]]) {
                        REPrimaryClassification * primaryModel = [[REPrimaryClassification alloc] init];
                        primaryModel.firstTypeId = dict[@"firstTypeId"];
                        primaryModel.firstTypeName = dict[@"firstTypeName"];
                        primaryModel.firstTypeImg = dict[@"firstTypeImg"];
                         [weakSelf.dataArr addObject:primaryModel];
                    }
                }
            }
            REPrimaryClassification * model  = weakSelf.dataArr[0];
            [weakSelf creatBaseUI];
            [weakSelf requestTwo:model.firstTypeId];
        }else
        {
            [SVProgressHUD showErrorWithStatus:responseObject[@"resultNote"]];
        }

    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"查看网络！！！"];
    }];
}
-(void)requestTwo:(NSString *)idStr
{
    
    __weak typeof(self) weakSelf  =  self;
    NSMutableDictionary * dict = [NSMutableDictionary new];
    dict[@"cmd"] = @"getSecondType";
    dict[@"firstTypeId"] = idStr;
    
    [self.dataTwoArr removeAllObjects];
    [RECheckLogin POST:REBaseUrl parameters:dict progress:^(CGFloat prosser) {
        
    } success:^(id responseObject) {
        if ([responseObject[@"result"] isEqualToString:@"0"]) {
            for (NSDictionary * dict in responseObject[@"secondTypeList"]) {
                for (NSInteger i = 0; i<weakSelf.secondArr.count; i++) {
                    if ([weakSelf.secondArr[i] isEqualToString:dict[@"secondTypeId"]])
                    {
                        RESecondaryClassification * secondaryClassModel = [[RESecondaryClassification alloc] init];
                        secondaryClassModel.secondTypeId = dict[@"secondTypeId"];
                        secondaryClassModel.secondTypeName = dict[@"secondTypeName"];
                        secondaryClassModel.secondTypeImg = dict[@"secondTypeImg"];
                        [weakSelf.dataTwoArr addObject:secondaryClassModel];
                    }
                }
            }
            [weakSelf.middleCollectionView reloadData];
            [weakSelf.bottomCollectionView reloadData];
            if (weakSelf.dataTwoArr.count) {
                
                RESecondaryClassification * model  = weakSelf.dataTwoArr[0];
                [weakSelf requestthird:model.secondTypeId];
                
            }
        }else
        {
            [SVProgressHUD showErrorWithStatus:responseObject[@"resultNote"]];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"查看网络！！！"];
    }];
}

-(void)requestthird:(NSString*)idStr
{
    __weak typeof(self) weakSelf  =  self;
    NSMutableDictionary * dict = [NSMutableDictionary new];
    dict[@"cmd"] = @"getThirdType";
    dict[@"secondTypeId"] = idStr;
    [self.thirdArr removeAllObjects];
    
    [RECheckLogin POST:REBaseUrl parameters:dict progress:^(CGFloat prosser) {
        
    } success:^(id responseObject) {
        if ([responseObject[@"result"] isEqualToString:@"0"]) {
            [SVProgressHUD dismiss];
            for (NSDictionary * dict in responseObject[@"thirdTypeList"]) {
                for (NSInteger i = 0; i<weakSelf.thirdDataArr.count; i++) {
                    if ([weakSelf.thirdDataArr[i] isEqualToString:dict[@"thirdTypeId"]])
                    {
                        REThirdClassification * secondaryClassModel = [[REThirdClassification alloc] init];
                        secondaryClassModel.thirdTypeId = dict[@"thirdTypeId"];
                        secondaryClassModel.thirdTypeName = dict[@"thirdTypeName"];
                        secondaryClassModel.thirdTypeImg = dict[@"thirdTypeImg"];

                        [weakSelf.thirdArr addObject:secondaryClassModel];
                    }
                }
            }
            [weakSelf.bottomCollectionView reloadData];
        }else
        {
            [SVProgressHUD showErrorWithStatus:responseObject[@"resultNote"]];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"查看网络！！！"];
    }];
}
-(void)creatBaseUI
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //    // 横向左右间距
    flowLayout.minimumInteritemSpacing = 0;
    // 上线间距
    flowLayout.minimumLineSpacing = 0;
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    self.topCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 70, REScreenW, 50) collectionViewLayout:flowLayout];
    self.topCollectionView.tag = 1001;
    self.topCollectionView.backgroundColor = [UIColor whiteColor];
    self.topCollectionView.delegate = self;
    self.topCollectionView.dataSource = self;
    [self.topCollectionView registerNib:[UINib nibWithNibName:@"REAlbumNameCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"REAlbumNameCollectionViewCell"];
    [self.view addSubview:self.topCollectionView];
    UIView * backView = [[UIView alloc] init];
    backView.frame = CGRectMake(0, 110, REScreenW, 3);
    backView.backgroundColor = RERGBColor(241, 241, 241);
    backView.alpha = 0.3;
    [self.view addSubview:backView];
    
    UICollectionViewFlowLayout *flowLayoutTwo = [[UICollectionViewFlowLayout alloc] init];
    //    // 横向左右间距
    flowLayoutTwo.minimumInteritemSpacing = 0;
    // 上线间距
    flowLayoutTwo.minimumLineSpacing = 0;
    [flowLayoutTwo setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    self.middleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 120, REScreenW, 100) collectionViewLayout:flowLayoutTwo];
    self.middleCollectionView.backgroundColor = [UIColor whiteColor];
    self.middleCollectionView.tag = 1002;
    self.middleCollectionView.delegate = self;
    self.middleCollectionView.dataSource = self;
    [self.middleCollectionView registerNib:[UINib nibWithNibName:@"REAlbumNameTwoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"REAlbumNameTwoCollectionViewCell"];
    [self.view addSubview:self.middleCollectionView];
    UICollectionViewFlowLayout *flowLayoutThird = [[UICollectionViewFlowLayout alloc] init];
    //    // 横向左右间距
    flowLayoutThird.minimumInteritemSpacing = 0;
    // 上线间距
    flowLayoutThird.minimumLineSpacing = 0;
    //    [flowLayoutThird setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    self.bottomCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 225, REScreenW, REScreenH-225) collectionViewLayout:flowLayoutThird];
    self.bottomCollectionView.tag = 1003;
    self.bottomCollectionView.backgroundColor = [UIColor whiteColor];
    self.bottomCollectionView.delegate = self;
    self.bottomCollectionView.dataSource = self;
    [self.bottomCollectionView registerNib:[UINib nibWithNibName:@"REAlbumNameThirdCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"REAlbumNameThirdCollectionViewCell"];
    [self.view addSubview:self.bottomCollectionView];
    UIView * middleBackView = [[UIView alloc] init];
    middleBackView.frame = CGRectMake(0, 218, REScreenW, 3);
    middleBackView.backgroundColor = RERGBColor(241, 241, 241);
    middleBackView.alpha = 1;
    [self.view addSubview:middleBackView];
}

#pragma -mark CollectionView DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    //    UICollectionView * isCollectionVIew = (UICollectionView*)[self.view viewWithTag:1001];
    switch (collectionView.tag) {
        case 1001:
        {
            return self.dataArr.count;
        }
            break;
        case 1002:
        {
            return self.dataTwoArr.count;
        }
            break;
        case 1003:
        {
            return self.thirdArr.count;
        }
            break;
        default:
            break;
    }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    switch (collectionView.tag) {
        case 1001:
        {
            REAlbumNameCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"REAlbumNameCollectionViewCell" forIndexPath:indexPath];
            REPrimaryClassification * albumModel = self.dataArr[indexPath.row];
            [cell showData:albumModel];
            if (indexPath.row == 0 && self.isSelect == YES) {
                cell.baseView.hidden = NO;
            }
            return cell;
        }
            break;
        case 1002:
        {
            REAlbumNameTwoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"REAlbumNameTwoCollectionViewCell" forIndexPath:indexPath];
            RESecondaryClassification * albumModel = self.dataTwoArr[indexPath.row];
            [cell showDateWithModel:albumModel];
            return cell;
        }
            break;
        case 1003:
        {
            REAlbumNameThirdCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"REAlbumNameThirdCollectionViewCell" forIndexPath:indexPath];
            REThirdClassification * albumModel = self.thirdArr[indexPath.row];
            [cell showDataThirdModel:albumModel];
            return cell;
        }
            break;
        default:
            break;
    }
    return nil;
}
//布局
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    switch (collectionView.tag) {
        case 1001:
        {
            return 20;
        }
            break;
        case 1002:
        {
            return 20;
        }
            break;
        case 1003:
        {
            return 5;
        }
            break;
        default:
            break;
    }
    
    return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    switch (collectionView.tag) {
        case 1001:
        {
            return 10;
        }
            break;
        case 1002:
        {
            return 10;
        }
            break;
        case 1003:
        {
            return 5;
        }
            break;
        default:
            break;
    }
    
    return 0;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    switch (collectionView.tag) {
        case 1001:
        {
            return CGSizeMake(125, 50);
        }
            break;
        case 1002:
        {
            return CGSizeMake(90, 90);
        }
            break;
        case 1003:
        {
            return CGSizeMake(180 , 150);
        }
            break;
        default:
            break;
    }
    return CGSizeMake(0, 0);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    switch (collectionView.tag) {
        case 1001:
        {
            return UIEdgeInsetsMake(5, 5, 5, 5);
        }
            break;
        case 1002:
        {
            return UIEdgeInsetsMake(5, 5, 5, 5);
        }
            break;
        case 1003:
        {
            return UIEdgeInsetsMake(5,5, 5,5);
        }
            break;
        default:
            break;
    }
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self) weakSelf = self;
    switch (collectionView.tag) {
        case 1001:
        {
            REAlbumNameCollectionViewCell * cell = (REAlbumNameCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
            
            NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
            REAlbumNameCollectionViewCell * zeroCell =  (REAlbumNameCollectionViewCell*)[collectionView cellForItemAtIndexPath:ip];
            if (indexPath.row == 0) {
                zeroCell.selected = NO;
                
            }else
            {
                cell.selected = YES;
                zeroCell.selected = NO;
                weakSelf.isSelect = NO;
            }
            REPrimaryClassification * model = self.dataArr[indexPath.row];
            [weakSelf requestTwo:model.firstTypeId];
        }
            break;
        case 1002:
        {
            RESecondaryClassification * model = self.dataTwoArr[indexPath.row];
            [weakSelf requestthird:model.secondTypeId];
        }
            break;
        case 1003:
        {
            REThirdClassification * model = self.thirdArr[indexPath.row];
            if (_albumBlockTwo) {
                _albumBlockTwo(model);
            }
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
